import { generateColumns, generateDictData } from '@/utils/plusUtil.js'
import { list } from '@/api/monitor/online.js';

export const useCommonConfig = ()=>{

  // 基础配置
  const { proxy } = getCurrentInstance()
  const primaryKey = ''
  const moduleName =  '在线用户'
  const apiFn = {
    listApiFn: list,
  }


  return {
    proxy,
    primaryKey,
    moduleName,
    apiFn,
  }

}


/**
 * 获取列和字典
 * @param isForm
 * @param data  表单数据对象
 */
export const useColumnsDict = (isForm = false, data) => {
  const { proxy } = getCurrentInstance()
  let formData = ref({})
  if (isForm) {
    if (!data || !isRef(data)) {
      console.error('当 isForm 为 true 时，data 必须是一个 ref 对象')
      return
    }
    formData = data
  }

  const dictRawData  = proxy.useDict(...[
    'sys_oper_type',
    'sys_common_status'
  ])

  // 拓展了propInTable，propInForm
  // 定义在这个里面可以对外层进行覆盖(根据是列表还是表单)
  // orderInForm 可以调整在表单里面显示顺序
  const columnsData = [
    {
      label: "会话编号",
      prop: "tokenId",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "登录账号",
      prop: "userName",
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "所属部门",
      prop: "deptName",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "主机",
      prop: "ipaddr",
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "登录地点",
      prop: "loginLocation",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "操作系统",
      prop: "os",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "浏览器",
      prop: "browser",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "登录时间",
      prop: "loginTime",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      },
      render: val=> proxy.parseTime(val)
    },
    {
      label: "操作",
      prop: "action",
      width: 160,
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    }
  ]
  const columns = ref([])
  columns.value = generateColumns(columnsData, isForm)
  const dictData = computed(()=>generateDictData(dictRawData))
  return {
    dictData,
    columns,
  }
}

